﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Data;
using Oracle.ManagedDataAccess.Client;
using System.Reflection;

namespace CP.DBService
{
    public class ServiceHelper
    {
        //获取distinct值
        public static IList<T> DistinctField<T>(string field, string strwhere)
        {
            DBHelper command = DBHelper.CreateInstance();
            IList<T> entities = new List<T>();
           
            string strSQL = string.Format("SELECT DISTINCT {0} as {0} FROM {1} " + strwhere + " order by {0} desc", field, typeof(T).Name);

            DataTable dts = command.ExecuteDataTable(strSQL);
            foreach (DataRow row in dts.Rows)
            {
                T entity = Activator.CreateInstance<T>();

                var property = entity.GetType().GetProperty(field, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                property.SetValue(entity, row[field], null);
                entities.Add(entity);
            }
            return entities;
        }
         
    }
}
